//
//  SectionController.m
//  UEAssistant
//
//  Created by mac on 2017/4/13.
//  Copyright © 2017年 youyixinxi. All rights reserved.
//

#import "SectionController.h"
#import "YZSortCell.h"
extern NSString * const YZUpdateMenuTitleNote;
static NSString * const ID = @"cell";

@interface SectionController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic, copy) NSMutableArray *titleArray;
@property (nonatomic, strong) NSMutableArray    *idArray;
// 记录选中的cell
@property (nonatomic, assign) NSInteger selectedCol;
@end

@implementation SectionController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:_selectedCol inSection:0];
    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _titleArray = [NSMutableArray array];
    _idArray = [NSMutableArray array];
    
    [AvailableNetWorking postWithUrl:groups_connector params:@{@"teamId":shareData.team_Id} successHandler:^(id responseObject) {
        
        NSLog(@"groups == %@",responseObject);
        if ([responseObject[@"code"] intValue] == 200) {
            NSArray *arr = responseObject[@"result"];
            [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
               
                [_titleArray addObject:obj[@"groupName"]];
                [_idArray addObject:[NSString stringWithFormat:@"%@",obj[@"id"]]];
            }];
            [_titleArray addObject:@"分组"];
            [_idArray addObject:@"分组"];
            _selectedCol = 0;
            [self.tableView reloadData];
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:_selectedCol inSection:0];
            [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        }else{
            NSLog(@"分组信息接收失败");
        }
    } falureHandler:^(NSError *error) {
        
        NSLog(@"分组信息接收失败");
    }];
    
    [self.tableView registerClass:[YZSortCell class] forCellReuseIdentifier:ID];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YZSortCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.textLabel.text = _titleArray[_titleArray.count - indexPath.row - 1];
    if (_titleArray.count == 0) {
        NSLog(@"数据加载中...");
    }else{
        if (indexPath.row == 0) {
            [cell setSelected:YES animated:NO];
        }
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _selectedCol = indexPath.row;
    NSString *type = _idArray[_idArray.count - indexPath.row - 1];
    if (self.didSelectSectionHandler != nil) {
        self.didSelectSectionHandler(type);
    }
    // 更新菜单标题
    [[NSNotificationCenter defaultCenter] postNotificationName:YZUpdateMenuTitleNote object:self userInfo:@{@"title":@"分组"}];
    
    
}

- (void)setDidSelectDayHandler:(DidSelectSectionHandler)didSelectSectionHandler {
   
    _didSelectSectionHandler = didSelectSectionHandler;
    self.didSelectDayHandler = _didSelectSectionHandler; // 传递 block
    
}


@end
